/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-07-06 09:53:45
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-07-06 10:12:18
 * @FilePath: /lesson/lesson_net/blog_cal_http/ClientMain.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "Protocol.hpp"

using namespace std;

int main(int argc, char argv[])
{
    if (argc != 3)
    {
        cerr << "Usage:" << argv[0] << "server_ip server_port" << endl;
        exit(-1);
    }

    char server_ip = argv[1];
    int server_port = argv[2];

    // 1、创建套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        cerr << "socket fail" << endl;
        exit(-2);
    }

    // 2、连接服务器
    struct sockaddr_in peer;
    memset(&peer, 0, sizeof(peer));

    peer.sin_family = AF_INET;
    peer.sin_port = htons(server_port);
    peer.sin_addr.s_addr = inet_addr(server_ip.c_str());

    if (connect(sock, (struct sockaddr *)&peer, sizeof(peer)) < 0)
    {
        cerr << "connect failed" << endl;
        exit(-3);
    }

    // 3、发起请求
    while (true)
    {
        // 构建请求
        request_t rq;
        cout << "请输入左操作数#";
        cin >> rq.x;
        cout << "请输入右操作数#";
        cin >> rq.y;

        cout << "请输入所要进行的运算符[+-*/%]#";
        cin >> rq.op;

        send(sock, &rq, sizeof(rq), 0);

        // 接收请求响应
        response_t rp;
        recv(sock, &rp, sizeof(rp), 0);

        cout << "status: " << rp.code << endl;

        cout << rq.x << rq.op << rq.y << "=" << rp.result << endl;
    }

    return 0;
}
